﻿using AutoMapper;
using Windows.Infrastructure.Share.Extensions;
using Windows.WorkFlow.Domain;

namespace Windows.WorkFlow.Service
{
    public class DomainToDtoMappingProfile : Profile
    {
        /// <summary>
        /// 配置构造函数，用来创建关系映射
        /// </summary>
        public DomainToDtoMappingProfile()
        {
            CreateMap<Form, FormResponse>();
            CreateMap<FlowCategory, FlowCategoryResponse>();
            CreateMap<Flow, FlowResponse>().ForMember(
               dto => dto.FlowCategoryName,
               domain => domain.MapFrom(src => src.FlowCategory!.Name)
            ).ForMember(
               dto => dto.FormName,
               domain => domain.MapFrom(src => src.Form!.Name)
            ).ForMember(
               dto => dto.RoleIds,
               domain => domain.MapFrom(src => src.Master_Flows.Where(x => x.Type == MasterEnum.Role.ToInt()).Select(s => s.MasterId))
            ).ForMember(
               dto => dto.OrganizationIds,
               domain => domain.MapFrom(src => src.Master_Flows.Where(x => x.Type == MasterEnum.Organization.ToInt()).Select(s => s.MasterId))
            ).ForMember(
               dto => dto.PostIds,
               domain => domain.MapFrom(src => src.Master_Flows.Where(x => x.Type == MasterEnum.Post.ToInt()).Select(s => s.MasterId))
            ).ForMember(
               dto => dto.PostLevelIds,
               domain => domain.MapFrom(src => src.Master_Flows.Where(x => x.Type == MasterEnum.PostLevel.ToInt()).Select(s => s.MasterId))
            ).ForMember(
               dto => dto.EmployeeIds,
               domain => domain.MapFrom(src => src.Master_Flows.Where(x => x.Type == MasterEnum.Employee.ToInt()).Select(s => s.MasterId))
            );

            CreateMap<Flow, FlowPublishResponse>().ForMember(
               dto => dto.FormEditorJson,
               domain => domain.MapFrom(src => src.Form!.EditorJson)
            ).ForMember(
               dto => dto.FormEditorOptionJson,
               domain => domain.MapFrom(src => src.Form!.EditorOptionJson)
            );
        }
    }
}
